package com.framework.Flow;

import com.framerock.baselib.CDate;
import com.framerock.baselib.CLog;
import com.framerock.baselib.CallBack;
import com.framerock.baselib.Rock;
import com.framerock.baselib.RockList;
import com.framework.ACore.Model;
import com.framework.Model.ModelDept;
import com.framework.Model.ModelUsera;

import java.util.ArrayList;
import java.util.Map;

public class FlowBill   extends FlowBase{

    public Model billModel;

    /**
     * 单据的数据
     * */
    public Map<String, String> billrs;

    /**
     * 单据id
     * */
    public String billid = "0";

    public FlowBill(Flow flows) {
        super(flows);
        billModel = Model.get(this.nDb, "flowbill");
    }

    /**
     * 信息单据的，不存在就更新
     * */
    public void info()
    {
        billrs = billModel.getOne(mFlow.mwhere);
        Map<String, String> da,ars,drs;
        if(billrs == null){
            String aid = Rock.getMapString(mFlow.data, "aid", "0"),adeptid = Rock.getMapString(mFlow.data, "adeptid", "0"),aname = "",adeptname = "",optname = "",applydt = Rock.getMapString(mFlow.data, "applydt");
            String optid = Rock.getMapString(mFlow.data, "optid", mRequest.USEAID);
            String optdt = mFlow.getFieldsval("optdt");
            if(Rock.isEmpt(optdt))optdt = mFlow.now;
            if(Rock.isEmpt(applydt))applydt = optdt.substring(0, 10);

            da = Rock.getMap();
            da.put("fmodename", mFlow.unitrs.get("name"));
            da.put("fmodenum", mFlow.modenum);
            da.put("fmodeid", mFlow.unitrs.get("id"));
            da.put("table", mFlow.table);
            da.put("mid", mFlow.mid);

            da.put("status", mFlow.billStatus()+"");
            da.put("cid", mFlow.getFieldsval("cid"));
            da.put("lastdt", mFlow.now);
            da.put("optdt", optdt);
            da.put("createdt", mFlow.now);
            String sericnum = this.createSericnum();
            da.put("sericnum", sericnum);
            da.put("aid", aid);

            if(!aid.equals("0")){
                ars         = mFlow.modelUsera.getXinxi(aid);
                adeptname   = Rock.getMapString(ars, "deptname");
                aname       = Rock.getMapString(ars, "name");
                if(adeptid.equals("0")){
                    adeptid = Rock.getMapString(ars, "deptid");
                }
                if(!adeptid.equals("0")){
                    drs         = mFlow.modelUsera.dept().getXinxi(adeptid);
                    adeptname   = Rock.getMapString(drs, "name");
                }
            }
            if(!optid.equals("0")){
                ars         = mFlow.modelUsera.getXinxi(optid);
                optname     = Rock.getMapString(ars, "name");
            }
            if(Rock.isEmpt(applydt))applydt = CDate.date();
            String summary = Rock.getMapString(mFlow.moders, "summary");
            da.put("content", Rock.replaceString(summary, mFlow.data));
            da.put("optid", optid);
            da.put("adeptid", adeptid);
            da.put("aname", aname);
            da.put("adeptname", adeptname);
            da.put("applydt", applydt);
            da.put("optname", optname);
            this.billid = billModel.insert(da)+"";
            billrs      = da;
        }else {
            this.billid = billrs.get("id");
            int zt = mFlow.billStatus();
            int ozt= Rock.getMapint(billrs, "status");
            if(zt != ozt){
                billModel.update("`status`="+zt+"", billid);
            }
        }
        mFlow.billrs = billrs;
    }

    /**
     * 创建流程单位
     * */
    public String createSericnum()
    {
        String num = "";

        return num;
    }

    /**
     * 提交到流程里
     * */
    public void submitFlow()
    {
        RockList rocklist = mFlow.mFlowBase.Course().getFlowCourse();
        Map<String, String> da = rocklist.getMap();
        da.put("nstatus", "0");
        da.put("status", "0");
        this.updateBill(da, true);
        mFlow.flowTodo(FlowTodo.ACTTYPE_TURN, da.get("nowchecknameid"));
    }

    /*
    * 更新单据流程
    * */
    public void updateBill(Map<String, String> da, Boolean upbool)
    {
        if(!Rock.getMapString(billrs, "nowcourseid").equals(da.get("nowcourseid")))upbool = true;
        if(!Rock.getMapString(billrs, "nowchecknameid").equals(da.get("nowchecknameid")))upbool = true;
        if(!Rock.getMapString(billrs, "nowcheckname").equals(da.get("nowcheckname")))upbool = true;
        if(!Rock.getMapString(billrs, "allcheckid").equals(da.get("allcheckid")))upbool = true;
        if(upbool){
            da.put("lastdt", mFlow.now);
            billModel.update(da, billid);
        }
    }

    /*
     * 更新单据流程
     * */
    public void updateBills(Map<String, String> da)
    {
        billModel.update(da, billid);
    }

    /**
     * 获取所有的单据
     * */
    public RockList getBilllist( String ids)
    {
        final RockList rocklist  = new RockList();
        ArrayList<Map<String, String>> rows = billModel.getAll("`table`='"+mFlow.table+"' AND `mid` IN("+ids+") AND `cid`="+COMPANYID+"", "mid,aname,adeptname,sericnum,nowcheckname", "",0, new CallBack(){
            @Override
            public Map<String, String> backMap(Map<String, String> a) {
                rocklist.addKeyMap(a.get("mid"), a);
                return super.backMap(a);
            }
        });
        rocklist.setDataList(rows);
        return rocklist;
    }
}
